﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class GroupInRoleDAO : SqlDAO
    {
        public const string SP_CREATE_OR_UPDATE_GROUP_IN_ROLE = "spCreateOrUpdateGroupInRole";
        public const string SP_GET_LIST_PAGING_GROUP_IN_ROLE = "spGetListPagingGroupInRole";
        public const string SP_GET_GROUP_IN_ROLE_BY_ID = "spGetGroupInRoleById";
        public const string SP_DELETE_GROUP_IN_ROLE = "spDeleteGroupInRole";
        public const string SP_GET_GROUP_IN_ROLE_BY_GROUP_ID = "spGetAllGroupInRoleByGroupId";
        public const string SP_GET_GROUP_IN_ROLE_BY_ROLE_ID = "spGetAllGroupInRoleByRoleId";
        public const string Param_GroupInRoleId = "@GroupInRoleId";
        public const string Param_UserId = "@UserId";
        public const string Param_GroupId = "@GroupId";
        public const string Param_GroupCode = "@GroupCode";
        public const string Param_RoleId = "@RoleId";
        public const string Param_RoleCode = "@RoleCode";
        public const string Param_AppID = "@AppID";


        public GroupInRoleDAO(String connectionString, System.Type type = null)
            : base(connectionString, type)
        {

        }

        internal Common.DataPage<GroupInRoleModel> GetDynamicListPaging(Models.Admin.GroupInRoleModel model, int pageSize, int pageIndex, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_GROUP_IN_ROLE);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_GroupId, model.GroupID));
                    command.Parameters.Add(CreateParameter(Param_RoleId, model.RoleID));
                    //command.Parameters.Add(new SqlParameter(Param_AppID, model.AppID));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<GroupInRoleModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal GroupInRoleModel GetById(int? groupInRoleId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_GROUP_IN_ROLE_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_GroupInRoleId, groupInRoleId));

                return GetSingle<GroupInRoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateGroupInRole(GroupInRoleModel GroupInRoleModel, int[] listID)
        {
            try
            {
                foreach (int id in listID)
                {
                    SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_GROUP_IN_ROLE);
                    command.Parameters.Add(new SqlParameter("@" + GroupInRoleModel.FIELD_GROUP_ID, id));
                    command.Parameters.Add(new SqlParameter("@" + GroupInRoleModel.FIELD_ROLE_ID, GroupInRoleModel.RoleID));
                    command.Parameters.Add(new SqlParameter("@" + GroupInRoleModel.FIELD_DESCRIPTIONS, GroupInRoleModel.Descriptions));
                    if (ExecuteQuery(command) <= 0)
                    {
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteGroupInRole(int[] groupInRoleId)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_GROUP_IN_ROLE);
                command.Parameters.Add(new SqlParameter("@GroupInRoleList", GetTableId<int>(groupInRoleId, "MenuInRoleID")));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal List<GroupInRoleModel> GetAllGroupInRoleByGroupId(int groupId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_GROUP_IN_ROLE_BY_GROUP_ID);
                command.Parameters.Add(CreateParameter(Param_GroupId, groupId));
                return GetList<GroupInRoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<GroupInRoleModel> GetAllGroupInRoleByRoleId(int roleId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_GROUP_IN_ROLE_BY_ROLE_ID);
                command.Parameters.Add(CreateParameter(Param_RoleId, roleId));
                return GetList<GroupInRoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

    }
}
